﻿@using SIGBugs.Shared.ViewModels
@using Microsoft.AspNetCore.Blazor.Browser.Interop;
@page "/fetchproject"
@inject HttpClient Http
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper

<h1>项目管理</h1>
<p>This component demonstrates fetching Project data from the server.</p>
<p class="text-right">
    <a href="/addproject" class="btn btn-primary btn-sm"><span class="oi oi-plus"></span> 创建新项目</a>
</p>
@if (empList == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <table class='table'>
        <thead>
            <tr>
                <th>ID</th>
                <th>项目名称</th>
                <th>描述</th>
                <th>创建时间</th>
                <th>创建者</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var emp in empList)
            {
                <tr>
                    <td>@emp.Id</td>
                    <td> <a href='/fetchtask/@emp.Id'>@emp.Name</a></td>
                    <td>@emp.Description</td>
                    <td>@string.Format("{0:d}", emp.CreatedDate)</td>
                    <td>@emp.CreatedBy</td>
                    <td>
                        <a href='/editproject/@emp.Id' class='btn btn-primary btn-sm'>编辑</a>
                        <a href='/delete/@emp.Id' class='btn btn-danger btn-sm'>删除</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>
}
@functions {
    List<ProjectVM> empList;
    protected override async Task OnInitAsync()
    {
        var token = SIGBugs.Client.Libs.JsInterop.GetToken();
        // RegisteredFunction.Invoke<bool>("log", token);
        if (string.IsNullOrEmpty(token))
        {
            UriHelper.NavigateTo("/login");
        }
        else
        {

            try
            {
                Http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
                empList = await Http.GetJsonAsync<List<ProjectVM>>("/api/Projects");
            }
            catch(Exception er)
            {
                //RegisteredFunction.Invoke<bool>("log", er.Message);
                UriHelper.NavigateTo("/login");
            }
        }

    }


}